var param = null;
var searchParam = {shop_name:"", username: "", state: ""};
$(function(){
	var o = {"":""};
	var data = AjaxUtil.command.executeQuery("sjc_index_shop_4");
	if(AjaxUtil.command.isSucceed(data)) {
		for(var i=0; i<data.rows.length; i++) {
			o[data.rows[i].goods_type_id] = data.rows[i].type_name;
		}
	}
	
	jQuery("#gridTable1").jqGrid({
		url: basePath + '/server/ajax.do',
		datatype: 'json',
		height: 250,
		autowidth: true,
	   	cellEdit: true, 
		cellsubmit: "clientArray",
	   	colNames:['店铺名称','开店帐号', '状态', '开店时间', '推荐类型', '店铺分类'],
	   	colModel:[
	   		{name:'shop_name',index:'s.shop_name', width:140},
	   		{name:'username',index:'t.username', width:60},
	   		{name:'state',index:'s.state', width:60, align:"center", formatter: function(cellvalue, options, rowObject) {
				if(cellvalue == 1){
					return "正常运营";
				} else {
					return "暂停运营";
				}
			}},
	   		{name:'add_time',index:'s.add_time', width:80, align:"center", formatter: function(cellvalue, options, rowObject) {
				return cellvalue.substring(0, 10);
			}},		
	   		{name:'goods_type_id', width:100, align:"center", editable:true, edittype:"select", editoptions:{value: o},
				formatter: function (cellvalue, options, rowObject) { return o[cellvalue]===undefined ? "" : o[cellvalue]; }},
	   		{name:'type_names', width:80, align:"left", sortable:false}
	   	],
	   	afterSaveCell: function(rowid, cellname, value, iRow, iCol) {
	   		var arrSql = [];
	   		arrSql.push({ sqlID: 'sjc_index_shop_3', parameters: {id: rowid} });
	   		if(value != "") {
	   			arrSql.push({ sqlID: 'sjc_index_shop_2', parameters: {id: rowid, goods_type_id: value} });
	   		}
	   		if(!AjaxUtil.command.isSucceed(AjaxUtil.command.executeTransaction(arrSql))) {
	   			$.jqalert("保存失败！");
	   		}
	   	},
	   	beforeRequest:function(){//向服务器端发起请求之前触发此事件但如果datatype是一个function时例外
        	param = $("#gridTable1").jqGrid("setGridParam")[0].p;
			$("#gridTable1").jqGrid("setGridParam", { //设置查询参数
				postData:{
					"iFunc": AjaxUtil.command.FUNC_PAGING,
					"strData": $.toJSON({
						sqlID: "sjc_index_shop_1",
						parameters: {sortname:param.sortname, sortorder:param.sortorder,
							shop_name: searchParam.shop_name,
							username: searchParam.username,
							state: searchParam.state
						},
						offset: (param.page-1)*param.rowNum,
						maxsize: param.rowNum
					})
				}
			});
		},
		jsonReader: {
			root: "query",			// 数据行（默认为：rows）
            page: function (obj) { // 当前页
            		return param.page;
            	},
            total: function (obj) { return Math.ceil(obj.total/param.rowNum); },			// 总页数
            records: "total",		// 总记录数
            repeatitems: false,
			id: "shop_id"
		},
	   	sortname: 's.add_time',
	    sortorder: "desc",
	   	rowNum:10,
	   	rowList:[10,20,30],
	   	pager: '#gridPager1',
	   	caption: "首页店铺推荐"
	});
	jQuery("#gridTable1").jqGrid("navGrid", "#gridPager1", {
		search : false, add : false, edit : false, del : false
    }).jqGrid('navButtonAdd',"#gridPager1",{
    	caption : '',
    	title : '查找',
    	position : 'first',
    	buttonicon : 'ui-icon-search',
    	onClickButton : openSearch
    });
    
    //设置弹出查询窗口参数
	$("#searchform").dialog({
        height:200,
        width:380,
        resizable:false,
        modal:true,  //这里就是控制弹出为模态
        buttons:{
            "取消":function(){$(this).dialog("close");},
            "重置":function(){
            	$("#searchform input").each(function(){
					$(this).val("");
				});
				$("#key_state").val("");
            },
            "查询":function(){
            	searchParam.shop_name = $("#key_shop_name").val();
            	searchParam.username = $("#key_username").val();
            	searchParam.state = $("#key_state").val();
            	$(this).dialog("close");
            	$("#gridTable1").jqGrid('setCaption', getTitle()).jqGrid('setGridParam').trigger("reloadGrid", [ {page: 1} ]);
            }
        }
    }).dialog("close");
    
	$("#gridTable1").jqGrid('setCaption', getTitle())
})

function openSearch() {
	$("#searchform").dialog("open");
}

function getTitle() {
	var s = "首页店铺推荐 【";
	if(searchParam.username != "") s += searchParam.username + "&nbsp;&nbsp;";
	else s += "所有用户&nbsp;&nbsp;";
	if(searchParam.shop_name != "") s += searchParam.shop_name + "&nbsp;&nbsp;";
	s += $("#key_state").find("option:selected").text();
	s += "】";
	return s;
}
